from fastapi import FastAPI, File, UploadFile
from flask import Flask, render_template, request
from flask.cli import load_dotenv
import erniebot

app = Flask(__name__)
load_dotenv()

def xieshi(img_str: str, tiaojian: str):
    img_str = img_str  # 这里需要图片识别的内容信息
    access_token = "50f1f18b30386d3c3f589905f5a5d06dfae8aa01"
    content = '根据' + img_str + tiaojian

    erniebot.api_type = 'aistudio'
    erniebot.access_token = access_token
    response = erniebot.ChatCompletion.create(
        model='ernie-bot',
        messages=[{'role': 'user', 'content': f"{content}”"}],
    )
    data_str = response.result
    xieshi = data_str.replace("\n", "  ")
    print(xieshi)
    return xieshi

@app.route('/', methods=['GET', 'POST'])
def result():
    if request.method == 'POST':
        product = request.form['product']
        tiaojian = request.form['tiaojian']
        answer = xieshi(product, tiaojian)
        return render_template('index.html', answer=answer)
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)